package com.vcyber.wty.service.impl;

import java.util.List;

import com.vcyber.wty.configure.InvoiceProperties;
import com.vcyber.wty.model.Enterprise;
import com.vcyber.wty.model.Invoice;
import com.vcyber.wty.model.InvoiceR;
import com.vcyber.wty.model.InvoiceRedReq;
import com.vcyber.wty.model.Receive;
import com.vcyber.wty.model.ResponseModel;
import com.vcyber.wty.service.InvoiceService;
import com.vpiaotong.openapi.OpenApi;
import com.vpiaotong.openapi.util.HttpUtils;
import com.vpiaotong.openapi.util.JsonUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Data
@NoArgsConstructor
public class InvoiceServiceImpl implements InvoiceService{

    private InvoiceProperties invoiceProperties;

    /**
     * 查询发票抬头
     */
    @Override
    public ResponseModel getTitleInfo(Enterprise enterprise) {
        //OpenApi参数内容(3des秘钥(票通提供),平台编码(票通提供),平台前缀(票通提供),私钥)
        String buildRequest = new OpenApi(
                invoiceProperties.getPassword(),
                invoiceProperties.getPlatform_code(),
                invoiceProperties.getPlatform_alias(),
                invoiceProperties.getPrivateKey()
        ).buildRequest(JsonUtil.toJson(enterprise));
        String response = HttpUtils.postJson(invoiceProperties.getGetTitleInfoUrl(), buildRequest);
        String responseString = new OpenApi(
                invoiceProperties.getPassword(),
                invoiceProperties.getPlatform_code(),
                invoiceProperties.getPlatform_alias(),
                invoiceProperties.getPrivateKey()
        ).disposeResponse(response, invoiceProperties.getPtPublicKey());
        JSONObject jsonObject = JSONObject.fromObject(responseString);
        ResponseModel responseModel = null;
        if (!"0000".equals(jsonObject.get("code"))) {
            Enterprise enterprise1 = (Enterprise) JSONObject.toBean(JSONObject.fromObject(jsonObject.get("content")), Enterprise.class);
            jsonObject.remove("content");
            responseModel = (ResponseModel) JSONObject.toBean(jsonObject, ResponseModel.class);
            responseModel.setContent(enterprise1);
        } else {
            List<Enterprise> enterpriseList = JSONArray.toList((JSONArray) jsonObject.get("content"), Enterprise.class);
            jsonObject.remove("content");
            responseModel = (ResponseModel) JSONObject.toBean(jsonObject, ResponseModel.class);
            responseModel.setContent(enterpriseList);
        }
        return responseModel;
    }

    /**
     * 註冊企業
     * @param enterprise
     * @return
     */
    @Override
    public ResponseModel registerEnterprise(Enterprise enterprise) {
        OpenApi openApi = new OpenApi(
                invoiceProperties.getPassword(),
                invoiceProperties.getPlatform_code(),
                invoiceProperties.getPlatform_alias(),
                invoiceProperties.getPrivateKey()
        );
        String buildRequest = openApi.buildRequest(JSONObject.fromObject(enterprise).toString());
        String respone = HttpUtils.postJson(invoiceProperties.getRegisterEnterpriseUrl(), buildRequest);
        ResponseModel responseModel = null;

        JSONObject jsonObject = JSONObject.fromObject(respone);
        Enterprise enterprise1 = (Enterprise) JSONObject.toBean((JSONObject) jsonObject.get("content"), Enterprise.class);
        jsonObject.remove("content");
        responseModel = (ResponseModel) JSONObject.toBean(jsonObject, ResponseModel.class);
        responseModel.setContent(enterprise1);
        return responseModel;
    }

    /**
     * 註冊回調地址 - 暫定
     */

    /**
     * 開具電子藍票
     */
    @Override
    public ResponseModel invoiceBlueOneItemAndPreferentialPolicy(Invoice invoice) {

        OpenApi openApi = new OpenApi(
                invoiceProperties.getPassword(),
                invoiceProperties.getPlatform_code(),
                invoiceProperties.getPlatform_alias(),
                invoiceProperties.getPrivateKey()
        );
        String buildRequest = openApi.buildRequest(JSONObject.fromObject(invoice).toString());
        String response = HttpUtils.postJson(invoiceProperties.getBlueInvoiceUrl(), buildRequest);
        String result = openApi.disposeResponse(response, invoiceProperties.getPtPublicKey());
        ResponseModel responseModel = null;
        JSONObject jsonObject  = JSONObject.fromObject(result);
        InvoiceR invoiceR = (InvoiceR) JSONObject.toBean((JSONObject) jsonObject.get("content"), InvoiceR.class);
        jsonObject.remove("content");
        responseModel = (ResponseModel) JSONObject.toBean(jsonObject, ResponseModel.class);
        responseModel.setContent(invoiceR);
        return responseModel;
    }

    /**
     * 開具紅票
     * @param invoiceRedReq
     * @return
     */
    @Override
    public ResponseModel invoiceRed(InvoiceRedReq invoiceRedReq) {
        OpenApi openApi = new OpenApi(
                invoiceProperties.getPassword(),
                invoiceProperties.getPlatform_code(),
                invoiceProperties.getPlatform_alias(),
                invoiceProperties.getPrivateKey()
        );
        String result = openApi.disposeResponse(
                HttpUtils.postJson(
                        invoiceProperties.getRedInvoiceUrl(),
                        openApi.buildRequest(JSONObject.fromObject(invoiceRedReq).toString())),
                invoiceProperties.getPtPublicKey());
        JSONObject jsonObject = JSONObject.fromObject(result);
        ResponseModel responseModel = null;
        InvoiceR invoiceR = (InvoiceR) JSONObject.toBean((JSONObject) jsonObject.get("content"), InvoiceR.class);
        jsonObject.remove("content");
        responseModel = (ResponseModel) JSONObject.toBean(jsonObject, ResponseModel.class);
        responseModel.setContent(invoiceR);
        return responseModel;
    }

    /**
     * 註冊推送
     */
    @Override
    public ResponseModel receiveInvoice(ResponseModel responseModel) {
        OpenApi openApi = new OpenApi(
                invoiceProperties.getPassword(),
                invoiceProperties.getPlatform_code(),
                invoiceProperties.getPlatform_alias(),
                invoiceProperties.getPrivateKey()
        );
        String result = openApi.disposeResponse(
                JSONObject.fromObject(responseModel).toString(),
                invoiceProperties.getPtPublicKey());
        JSONObject jsonObject = JSONObject.fromObject(result);

        ResponseModel resultResponseModel = null;
        Receive receive = (Receive) JSONObject.toBean((JSONObject) jsonObject.get("content"), Receive.class);
        jsonObject.remove("content");
        resultResponseModel = (ResponseModel) JSONObject.toBean(jsonObject, ResponseModel.class);
        resultResponseModel.setContent(receive);
        return resultResponseModel;
    }
}
